package com.css.apps.base.ip.action;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.css.apps.base.ip.model.SIp;
import com.css.db.page.Page;
import com.css.db.query.QueryCache;
import com.css.util.Messages;
import com.css.util.StringHelper;
import com.css.webbase.webwork.action.AdminAction;
import com.opensymphony.xwork.Action;

@SuppressWarnings("serial")
public class DirIp extends AdminAction {
	
	private static Log log = LogFactory.getLog(DirIp.class);
	private String uuid = null;
	private String ip = null;
	private String openFlag = null;
	private Page page;

	public DirIp() {
		page = new Page();
		page.setCountField("a.uuid");
	}

	@Override
	public String adminGo() {
		try {
			QueryCache qc = new QueryCache("select a.uuid from SIp a " + getWhere() + getOrder());
			setWhere(qc);
			page = qc.page(page);
			page.setResults(QueryCache.idToObj(SIp.class, page.getResults()));
			return Action.SUCCESS;
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			setMessage(Messages.getString("systemMsg.exception"));
			return Action.ERROR;
		}
	}

	public String getWhere() {
		StringBuffer sb = new StringBuffer(" where 1=1 ");
		if (StringHelper.isNotEmpty(ip))
			sb.append(" and a.ip like :ip ");
		if (StringHelper.isNotEmpty(openFlag))
			sb.append(" and a.openFlag = :openFlag ");
		return sb.toString();
	}

	public void setWhere(QueryCache qc) {
		if (StringHelper.isNotEmpty(ip))
			qc.setParameter("ip", "%" + ip + "%");
		if (StringHelper.isNotEmpty(openFlag))
			qc.setParameter("openFlag", openFlag);
	}

	public String getOrder() {
		return StringHelper.isNotEmpty(page.getOrderByString()) ? page.getOrderByString() : "order by a.openFlag";
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public String getUuid() {
		return uuid;
	}

	public void setUuid(String uuid) {
		this.uuid = uuid;
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public String getOpenFlag() {
		return openFlag;
	}

	public void setOpenFlag(String openFlag) {
		this.openFlag = openFlag;
	}
}
